/**
 * Created by zhangligang
 * Email: tzhang2450@163.com
 * Date: 2018/8/19
 * Time: 20:18
 */
$(function(){
    loadList('/v1/manager/program/'+program_id+'/user/list.json');

    $('#change-status-dialog').on('show.bs.modal', function(event){
        var button = $(event.relatedTarget);
        var id = button.data('id');
        var status = button.data('change-status');

        var puid = id;
        if (id == 'all') {
            puid = '';
            $('.select-check-time').each(function(){
                if ($(this).prop('checked')) {
                    puid += $(this).val() + ',';
                }
            });
        }
        $(this).find('.program-user-id').val(puid);
        $(this).find('.program-user-status').val(status);
        if (status == 2) {
            $(this).find('.set-select-time').removeClass('hidden');
            $(this).find('.start-end-time').removeAttr('disabled');
        }
        if (status == 1) {
            $('.change-status-tip').text('确定同意该用户申请吗?');
        }
        if (status == 3) {
            $('.change-status-tip').text('确定拒绝该用户申请吗?');
        }
    });
    $('#change-status-dialog .btn-save').on('click', function() {
        var data = {
            id: $('.program-user-id').val(),
            status: $('.program-user-status').val()
        };
        if (data.status == 2) {
            data.start_end_time = $('.start-end-time').val();
        }
        sendAjax('/v1/program/user/change/status', 'post', {data:data}, function(json) {
            if (data.status == 1 || data.status == 3) {
                var flag = data.status == 1 ? 1 : 2;
                sendAjax('/v1/manager/send/msg', 'post', {user_id:data.id, flag:flag}, function(json){});
            }
            location.reload();
        });
    });
    $('#change-status-dialog').on('hide.bs.modal', function(){
        $(this).find('.program-user-id').val('');
        $(this).find('.program-user-status').val('');
        $(this).find('.set-select-time').addClass('hidden');
        $(this).find('.start-end-time').val('').attr('disabled', 'disabled');
        $('.change-status-tip').text('');
    });

    $(document).on('change', '.select-check-time', function(){
        if ($('.select-check-time:checked').first().val() != undefined) {
            $('.set-all-time').removeClass('hidden');
        } else {
            $('.set-all-time').addClass('hidden');
        }
    });

    $(document).on('change', '.select-all-check', function(){
        var _change = this.checked;
        $('.select-check-time').each(function(){
            this.checked = _change;
            $(this).trigger('change');
        })
    });
});

function loadList(url) {
    $("#user-table").bootstrapTable({
        url: url,
        ajaxOptions:{
            headers: {"Authorization": $.cookie()._token}
        },
        pagination: true,
        toolbar: '<a class="btn btn-info set-all-time hidden" data-toggle="modal"  data-target="#change-status-dialog" data-id="all" data-change-status="2">统一设置选房时间</a>',
        iconSize: "outline",
        pageList: [10, 20, 50, 100],
        sortable: true,
        sortOrder: 'desc',
        striped: true,
        sortStable: true,
        sidePagination: 'server',
        icons: {
            refresh: "glyphicon-repeat",
            columns: "glyphicon-list"
        },
        columns: [{
            field: 'id',
            title: '<input type="checkbox" class="select-all-check" />&nbsp;ID',
            formatter: function (value) {
                return '<input type="checkbox" class="select-check-time" value="'+value+'" />&nbsp;'+value;
            }
        }, {
            field: 'realname',
            title: '申请人'
        }, {
            field: 'score',
            title: ' 分数',
            formatter: function (value) {
                return value == 0 ? '-' : value;
            }
        }, {
            field: 'status',
            title: '状态',
            formatter: function (value) {
                switch (value) {
                    case 0 : return '待审核';
                    case 1 : return '已通过';
                    case 2 : return '可选房';
                    case 3 : return '已拒绝';
                    default : return '待审核';
                }
            }
        }, {
            field: 'start_end_time',
            title: '选房时间',
            formatter: function (value, row) {
                return row.status == 2 ? value : '-';
            }
        }, {
            field: 'id',
            title: '操作',
            formatter: function (value, row) {
                var action = '';
                if (row.status == 0) {
                    action += '<a class="btn btn-xs btn-success" data-toggle="modal" data-target="#change-status-dialog" data-change-status="1" data-id="'+row.id+'">通过</a>';
                    action += '<a class="btn btn-xs btn-danger" data-toggle="modal" data-target="#change-status-dialog" data-change-status="3" data-id="'+row.id+'">拒绝</a>';
                } else if(row.status == 1) {
                    action += '<a class="btn btn-xs btn-info" data-toggle="modal" data-target="#change-status-dialog" data-change-status="2" data-id="'+row.id+'">设置选房时间</a>';
                    action += '<a class="btn btn-xs btn-danger" data-toggle="modal" data-target="#change-status-dialog" data-change-status="3" data-id="'+row.id+'">拒绝</a>';
                } else if (row.status == 2) {
                    action += '<a class="btn btn-xs btn-info" data-toggle="modal" data-target="#change-status-dialog" data-change-status="2" data-id="'+row.id+'">修改选房时间</a>';
                } else {
                    action += '<a class="btn btn-xs btn-success" data-toggle="modal" data-target="#change-status-dialog" data-change-status="1" data-id="'+row.id+'">通过</a>';
                }
                return action;
            }
        }]
    });
}